<?php
declare(strict_types=1);

namespace App\Controller\Backend;

use App\Logic\AdminLogic;
use App\Logic\AdminMenuLogic;
use App\Model\Category;
use App\Model\LiveColumn;
use App\Model\Time;
use App\Service\UploadService;
use Exception;
use Hyperf\HttpServer\Annotation\AutoController;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

#[AutoController(prefix: '/index', server: 'backend')]
class IndexController extends AbstractController
{
    /**
     * 初始化
     * @return ResponseInterface
     */
    public function init(): ResponseInterface
    {
        return $this->apiReturn(200, 'succ', [
            'homeInfo' => [
                'title' => '首页',
                'href' => '/pages/index.html',
            ],
            'logoInfo' => [
                'title' => '后台管理系统',
                'image' => 'images/logo.png',
                'href' => '',
            ],
            'menuInfo' => AdminMenuLogic::lists(true),
            'config' => [
                'liveColumns' => LiveColumn::query()->where('enable', 1)->orderBy('listorder')->orderBy('id')->get(['id', 'title'])->toArray(),
                'articleCategory' => Category::query()->orderBy('listorder')->orderBy('id')->get(['id', 'catname'])->toArray(),
                'sports' => [
                    ['id' => 1, 'name' => '足球'],
                    ['id' => 2, 'name' => '篮球'],
                    ['id' => 3, 'name' => '网球'],
                    ['id' => 10, 'name' => '排球'],
                ],
            ],
        ]);
    }

    /**
     * index
     * @return ResponseInterface
     */
    public function index(): ResponseInterface
    {
        return $this->apiReturn(200, 'succ');
    }

    /**
     * 清理缓存
     * @return ResponseInterface
     */
    public function clear(): ResponseInterface
    {
        return $this->apiReturn(200, '清理完成');
    }

    /**
     * 统计数据
     * @return ResponseInterface
     */
    public function statistics(): ResponseInterface
    {
        return $this->apiReturn(200, 'succ', AdminLogic::statistics());
    }

    /**
     * 帐号登录
     * @return ResponseInterface
     */
    public function login(): ResponseInterface
    {
        $input = $this->request->all();
        if (empty($input['username'])) {
            return $this->apiReturn(400, '用户名不能为空');
        }
        if (empty($input['password'])) {
            return $this->apiReturn(400, '密码不能为空');
        }
        try {
            $info = AdminLogic::login($input);
            return $this->apiReturn(200, '登录成功', $info);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 帐号退出
     * @return ResponseInterface
     */
    public function logout(): ResponseInterface
    {
        try {
            AdminLogic::logout();
            return $this->apiReturn(200, '退出成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 上传图片
     * @return ResponseInterface
     */
    public function uploadImage(): ResponseInterface
    {
        $file = $this->request->file('file');
        try {
            $data = $this->container->get(UploadService::class)->image($file);
            return $this->apiReturn(200, '上传成功', $data);
        } catch (Exception|NotFoundExceptionInterface|ContainerExceptionInterface $e) {
            return $this->apiReturn(400, '上传失败:' . $e->getMessage());
        }
    }
}